package com.filter;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

public class MyPreFilter extends ZuulFilter{

	public Object run() {		
		System.out.println("in myPreFilter, type is pre");
		
		SimpleDateFormat timeFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		System.out.println(timeFormat.format(new java.util.Date()));
		
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		String url = request.getRequestURI();
		System.out.println(url);
		
		//throw exception
//		int i = 0;
//		System.out.println(7/i);
//		
//		if( url.indexOf("hello") == -1 ){
//			System.out.println("Blocked by Pre Filter.");
//			ctx.setSendZuulResponse(false);
//			ctx.setResponseStatusCode(404);
//		}	
				
		return null;
	}

	public boolean shouldFilter() {
		return true;
	}

	@Override
	public int filterOrder() {
		return 1;
	}

	@Override
	public String filterType() {
		return "pre";
	}

}
